package com.hongsheng.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hongsheng.pojo.*;
import com.hongsheng.pojo.Package;
import com.hongsheng.pojo.vo.GoodsReportVo;
import com.hongsheng.util.GoodsSqlProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.data.relational.core.sql.In;

import java.util.List;

/**
 * TODO
 *
 * @author admin
 * @version 1.0
 * @date 2023/8/10 18:27
 */
@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {
    @Select("select * from tb_goods")
    @Results({
            @Result(id=true,property = "id",column = "id"),
            @Result(property = "order_id",column = "o_id"),
            @Result(property = "depository",column = "de_id", javaType = Depository.class,
                    one=@One(select =
                            "com.hongsheng.mapper.GoodsMapper.findDepositoryById")),
            @Result(property = "driver",column = "dr_id", javaType = Driver.class,
                    one=@One(select =
                            "com.hongsheng.mapper.DriverMapper.findDriverById")),
            @Result(property = "pack",column = "p_id", javaType = Package.class,
                    one=@One(select =
                            "com.hongsheng.mapper.GoodsMapper.findPackageById")),
            @Result(property = "name",column = "name"),
            @Result(property = "type",column = "type"),
            @Result(property = "weight",column = "weight"),
            @Result(property = "size",column = "size"),
            @Result(property = "quantity",column = "quantity"),
            @Result(property = "nature",column = "nature")
    })
    List<Goods> findAll();
    @Select("select * from tb_depository where id=#{id}")
    Depository findDepositoryById(int id);
    @Select("select * from tb_driver where id=#{id}")
    Driver findDriverById(int id);
    @Select("select * from tb_package where id=#{id}")
    Package findPackageById(int id);
    @Insert("insert into tb_goods values(null,#{order_id},#{depository.id},#{driver.id},#{name},#{type},#{weight},#{size},#{quantity},#{nature},#{pack.id})")
    Integer insertGoods(Goods goods);
    @Delete("delete from tb_goods where id=#{id}")
    Boolean deleteGoods(int id);
    @Update("update tb_goods set o_id=#{order_id},de_id=#{depository.id},dr_id=#{driver.id},p_id=#{pack.id},name=#{name},type=#{type},weight=#{weight},size=#{size},quantity=#{quantity},nature=#{nature} where id=#{id}")
    Integer updateGoods(Goods goods);

    //查询所有订单id
    @Select("select id from tb_order where order_status<3")
    List<Integer> getAllOrderId();

    //根据id查货物
    @Select("select * from tb_goods where id=#{id}")
    @Results({
            @Result(property = "order_id",column = "o_id"),
            @Result(property = "depository",column = "de_id", javaType = Depository.class,
                    one=@One(select =
                            "com.hongsheng.mapper.GoodsMapper.findDepositoryById")),
            @Result(property = "driver",column = "dr_id", javaType = Driver.class,
                    one=@One(select =
                            "com.hongsheng.mapper.DriverMapper.findDriverById")),
            @Result(property = "pack",column = "p_id", javaType = Package.class,
                    one=@One(select =
                            "com.hongsheng.mapper.GoodsMapper.findPackageById")),
            @Result(property = "name",column = "name"),
            @Result(property = "type",column = "type"),
            @Result(property = "weight",column = "weight"),
            @Result(property = "size",column = "size"),
            @Result(property = "quantity",column = "quantity"),
            @Result(property = "nature",column = "nature")
    })
    Goods findGoodsById(int id);

    //查询订单状态
    @Select("select order_status from tb_order where id=#{id}")
    Integer getOrderStatusById(int id);
//    //查询司机状态
//    @Select("select status from tb_driver where id=#{id}")
//    Integer getDriverStatusById(int id);

    @SelectProvider(type = GoodsSqlProvider.class,method = "getGoods")
    @Results({
            @Result(id=true,property = "id",column = "id"),
            @Result(property = "order_id",column = "o_id"),
            @Result(property = "depository",column = "de_id", javaType = Depository.class,
                    one=@One(select =
                            "com.hongsheng.mapper.GoodsMapper.findDepositoryById")),
            @Result(property = "driver",column = "dr_id", javaType = Driver.class,
                    one=@One(select =
                            "com.hongsheng.mapper.DriverMapper.findDriverById")),
            @Result(property = "pack",column = "p_id", javaType = Package.class,
                    one=@One(select =
                            "com.hongsheng.mapper.GoodsMapper.findPackageById")),
            @Result(property = "name",column = "name"),
            @Result(property = "type",column = "type"),
            @Result(property = "weight",column = "weight"),
            @Result(property = "size",column = "size"),
            @Result(property = "quantity",column = "quantity"),
            @Result(property = "nature",column = "nature")
    })
    List<Goods> getPageGoods(@Param("name") String name,
                              @Param("type") String type,
                              @Param("nature") String nature);


    //货物包装的成本
    @Select("select tp.cost*(sum(tg.size*tg.quantity)) as money from tb_goods as tg,tb_package as tp where tg.p_id=tp.id and tg.id=#{id}")
    Double getCost(Integer id);

    //货物包装的收入
    @Select("select tp.price*(sum(tg.size*tg.quantity)) as money from tb_goods as tg,tb_package as tp where tg.p_id=tp.id and tg.id=#{id}")
    Double getIncome(Integer id);

    //包装支出
    @Insert("insert into tb_bill values (null,'支出',#{billDesc},#{amount},SYSDATE())")
    Integer GoodsCost(Bill bill);

    //包装收入账单
    @Insert("insert into tb_bill values (null,'收入',#{billDesc},#{amount},SYSDATE())")
    Integer GoodsIncome(Bill bill);
    /**
     * 查询类型订单数量
     * @return
     */

    @Select("SELECT COUNT(1) AS order_number, type  FROM tb_goods GROUP BY type")
    @Results({
            @Result(property = "value",column = "order_number"),
            @Result(property = "name",column = "type")
    })
    List<GoodsReportVo> selectGoodsByQuantity();
}




